
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class BandejaDeEntradaCAD : BasicCAD, IBandejaDeEntradaCAD
        {
        public BandejaDeEntradaCAD() : base ()
        {
        }

        public BandejaDeEntradaCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public BandejaDeEntradaEN ReadOIDDefault (int id)
        {
                BandejaDeEntradaEN bandejaDeEntradaEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        bandejaDeEntradaEN = (BandejaDeEntradaEN)session.Load (typeof(BandejaDeEntradaEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in BandejaDeEntradaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return bandejaDeEntradaEN;
        }


        public int New_ (BandejaDeEntradaEN bandejaDeEntrada)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        if (bandejaDeEntrada.Usuario != null) {
                                bandejaDeEntrada.Usuario = (SpotifyGenNHibernate.EN.Spotify.UsuarioEN)session.Load (typeof(SpotifyGenNHibernate.EN.Spotify.UsuarioEN), bandejaDeEntrada.Usuario.Id);
                        }

                        session.Save (bandejaDeEntrada);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in BandejaDeEntradaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return bandejaDeEntrada.Id;
        }

        public void Destroy (int id)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        BandejaDeEntradaEN bandejaDeEntradaEN = (BandejaDeEntradaEN)session.Load (typeof(BandejaDeEntradaEN),
                                id

                                );
                        session.Delete (bandejaDeEntradaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in BandejaDeEntradaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public System.Collections.Generic.IList<BandejaDeEntradaEN> Consultar (int first, int size)
        {
                System.Collections.Generic.IList<BandejaDeEntradaEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        if (size > 0)
                                result = session.CreateCriteria (typeof(BandejaDeEntradaEN)).
                                         SetFirstResult (first).SetMaxResults (size).List<BandejaDeEntradaEN>();
                        else
                                result = session.CreateCriteria (typeof(BandejaDeEntradaEN)).List<BandejaDeEntradaEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in BandejaDeEntradaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        }
}
